package org.particles.domain;

/**
 * produces return values between 0 and 1. should be used
 * to determine field strength of particle field. the field
 * changes linear to distance.
 */
public class LinearFunction extends AbstractFunction {
	
	/**
	 * @param distance to which distance 
	 * the field strength should be 0. strength will be
	 * increase linear by distance less than the given distance.
	 */
	public LinearFunction(final double distance) {
		setFactor(distance);
	}
	
	/**
	 * @return value between 0 and 1.
	 */
	@Override
	public double calc(final double distance) {
		return calc(distance, getFactor());
	}

	@Override
	public double calc(final double distance, final double factor) {
		final double toReturn = -distance / factor + 1;
		if(toReturn < 0) {
			return 0;
		}
		else {
			return toReturn;
		}
	}
}
